#![allow(dead_code)]

use config::{Config, FileFormat};
use config_plus::Configuration;
use serde::Deserialize;

#[derive(Debug, Deserialize, Configuration)]
#[properties(file(path = "application.yml", require = true))]
struct ApplicationConfig {
    server: ServerConfig,
    database: DatabaseConfig,
}

#[derive(Debug, Deserialize)]
struct ServerConfig {
    port: u16,
    path: String,
}

#[derive(Debug, Deserialize)]
struct DatabaseConfig {
    deriver: String,
    host: String,
    name: String,
    password: String,
    database: String,
}

#[derive(Debug, Deserialize, Configuration)]
#[properties(prefix = "push.file", file(path = "application.yml", require = true))]
#[serde(rename_all(deserialize = "camelCase"))]
struct PushFile {
    enable: u8,
    app_id: String,
    app_key: String,
    master_secret: String,
    package_name: String,
}

/// yaml/yml配置
fn yml_config() -> ApplicationConfig {
    Config::builder()
        // Add in `./Settings.toml`
        .add_source(config::File::new(
            "config-lib/application.yml",
            FileFormat::Yaml,
        ))
        .add_source(config::File::from_str(
            r#"
            server: 
                path: "/file"
            "#,
            FileFormat::Yaml,
        ))
        // 添加环境变量的源，               前缀               前缀分隔符    即 APP_
        // .add_source(config::Environment::with_prefix("APP").prefix_separator("_"))
        .add_source(
            config::Environment::with_prefix("")
                .separator(".")
                .prefix_separator("")
                .list_separator(","), // list分隔符
        )
        .build()
        .unwrap()
        .try_deserialize::<ApplicationConfig>()
        .unwrap()
}

/// toml配置
fn toml_config() -> ApplicationConfig {
    Config::builder()
        // Add in `./Settings.toml`
        .add_source(config::File::new(
            "config-lib/application.toml",
            FileFormat::Toml,
        ))
        // 添加环境变量的源，               前缀               前缀分隔符    即 APP_
        // .add_source(config::Environment::with_prefix("APP").prefix_separator("_"))
        .add_source(
            config::Environment::with_prefix("")
                .prefix_separator("")
                .list_separator(","), // list分隔符
        )
        .build()
        .unwrap()
        .try_deserialize::<ApplicationConfig>()
        .unwrap()
}

fn main() {
    let config = PushFile::config();
    // let config = ApplicationConfig::config();
    println!("{:#?}", config);

    // let config = Config::builder()
    //     // Add in `./Settings.toml`
    //     .add_source(config::File::new(
    //         "config-lib/application.yml",
    //         FileFormat::Yaml,
    //     ))
    //     .build()
    //     .unwrap()
    //     .try_deserialize::<ApplicationConfig>()
    //     .unwrap();
    // println!("{:#?}", config);
}
